/*
 * @Description: 
 * @Version: 1.0
 * @Autor: laikt
 * @Date: 2021-01-11 11:54:04
 * @LastEditors: laikt
 * @LastEditTime: 2021-01-11 14:00:32
 */
import {
    Module,
    CacheModule,
    HttpModule,
    Global,
    CacheInterceptor,
    HttpModuleOptions
} from '@nestjs/common'
import { UserModule } from './logical/user/user.module'
import { AuthModule } from './logical/auth/auth.module'
import { TypeOrmConfigService } from './config/typeorm.service'
import { ConfigService } from './config/config.service'
import { MulterConfigService } from './config/multer.service'
import { CacheConfigService } from './config/cacheConfig.service'
import { ConfigModule } from './config/config.module'
import { TypeOrmModule } from '@nestjs/typeorm'
import { MulterModule } from '@nestjs/platform-express'
import { RoleModule } from './logical/role/role.module'
import { HttpConfigService } from './config/http.service'
import { APP_INTERCEPTOR } from '@nestjs/core'
import { ScheduleModule } from '@nestjs/schedule'
import { RedisModule, RedisModuleOptions } from 'nestjs-redis'
import { TableModule } from './logical/table/table.module';
import { UserController } from './logical/user/user.controller';

// @Global()
@Module({
    imports: [
        ConfigModule.register({ folder: './config' }),
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useClass: TypeOrmConfigService
        }),
        HttpModule.registerAsync({
            // imports: [ConfigModule],
            // inject: [ConfigService],
            // useClass: HttpConfigService,
            // imports: [ConfigModule],
            useClass: HttpConfigService
        }),
        CacheModule.registerAsync({
            useClass: CacheConfigService
        }),
        MulterModule.registerAsync({
            useClass: MulterConfigService
        }),
        RedisModule.forRootAsync({
            // imports: [ConfigModule],
            useFactory: (configService: ConfigService) =>
                ({
                    host: configService.get('REDIS_HOST'),
                    port: configService.getNumber('REDIS_PORT'),
                    db: configService.getNumber('REDIS_DB'),
                    password: configService.get('REDIS_PASSWORD'),
                    keyPrefix: configService.get('REDIS_PREFIX')
                } as RedisModuleOptions),
            inject: [ConfigService]
        }),
        // HttpModule.register({
        //     timeout: 5000,
        //     maxRedirects: 5,
        //     baseURL: 'http://192.168.1.107/api/v1.0'
        // }),

        // HTTP注册
        // HttpModule.registerAsync({
        //     imports: [ConfigModule],
        //     useFactory: async (configService: ConfigService) => ({
        //         timeout: 1,
        //         maxRedirects: 5,
        //         baseURL: 'http://192.168.1.107/api/v1.0'
        //     }),
        //     inject: [ConfigService]
        // }),
        ScheduleModule.forRoot(),
        RoleModule,
        AuthModule,
        UserModule,
        TableModule,
    ],
    controllers: [UserController],
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useClass: CacheInterceptor
        }
    ]
    // exports: [HttpConfigService]
})
export class AppModule {}
